package cn.gailvlun.gll.widget.edittext;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.support.v4.content.ContextCompat;
import android.text.InputFilter;
import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
import android.util.AttributeSet;

import com.scwang.smartrefresh.layout.util.DensityUtil;

import cn.gailvlun.gll.net.R;



public class PasswordEditText extends RightImageEditText {

    private boolean isVisible = true;
    private Drawable visibleDrawable;
    private Drawable invisibleDrawable;

    public PasswordEditText(Context context) {
        super(context);
    }

    public PasswordEditText(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public PasswordEditText(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

    }

    @Override
    public void init() {
        super.init();


        visibleDrawable = ContextCompat.getDrawable(getContext(), R.drawable.ic_password_selected);
        visibleDrawable.setBounds(0, 0, DensityUtil.dp2px(24), DensityUtil.dp2px(24));

        invisibleDrawable = ContextCompat.getDrawable(getContext(), R.drawable.ic_password_unselected);
        invisibleDrawable.setBounds(0, 0, DensityUtil.dp2px(24), DensityUtil.dp2px(24));

        setRightDrawable(visibleDrawable,
                DensityUtil.dp2px(24), DensityUtil.dp2px(24));
        setWhenFocusAppear(false);
        setTransformationMethod(HideReturnsTransformationMethod.getInstance());
        setOnRightClickListener(new OnRightClickListener() {
            @Override
            public void onRightClick(RightImageEditText riet) {
                isVisible = !isVisible;
                if (isVisible) {
                    
                    setRightDrawable(visibleDrawable);
                    setTransformationMethod(HideReturnsTransformationMethod.getInstance());

                } else {

                    
                    setRightDrawable(invisibleDrawable,
                            DensityUtil.dp2px(24), DensityUtil.dp2px(24));
                    setTransformationMethod(PasswordTransformationMethod.getInstance());

                }
                updateRightDrawable(true);
            }
        });
    }


    public void startFilter() {
        setFilters(new InputFilter[]{new PasswordFilter()});
    }

}
